package com.common;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by 10742 on 2017/7/9.
 */
public class MoleculeTool extends JdbcDaoSupport {

    public int insert(final int accountID, final String software, final String document, final String sampleID){
        final String sql = "insert into MolecuMaker_Resear(MonitPersonel_ID, Software, Reference, Sample_MeasureNO) values(?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setInt(1, accountID);
                ps.setString(2, software);
                ps.setString(3, document);
                ps.setString(4, sampleID);
                return ps;
            }
        }, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public int update(int moleculeID, int accountID, String software, String document){
        String sql = "update MolecuMaker_Resear set MonitPersonel_ID=?,Software=?,Reference=? where ID=?";
        return getJdbcTemplate().update(sql, accountID, software, document, moleculeID);
    }
}
